<h1>Au hasard</h1>

<?php
$req = $bdd->query('SELECT COUNT(id) AS nombre_total FROM chapitres');
$donnees = $req->fetch();
$nombre_total=($donnees['nombre_total']-1);
if ($nombre_total < 1) { $nombre_total=1; }
$hasard=rand(1,$nombre_total);

$req_cha = $bdd->prepare('SELECT * FROM chapitres WHERE id=:id');
$req_cha->execute(array('id' => $hasard));
$donnees_cha = $req_cha->fetch();
?>

<?php
$req_cat = $bdd->prepare('SELECT * FROM categories_textes WHERE id=:id');
$req_cat->execute(array('id' => $donnees_cha['categorie']));
$donnees_cat = $req_cat->fetch();

$req_txt = $bdd->prepare('SELECT * FROM textes WHERE livre=:id');
$req_txt->execute(array('id' => $donnees_cha['id']));
$donnees_txt = $req_txt->fetch();
 
?>

<p><?php echo $donnees_cat['nom']; ?> <b><?php echo $donnees_cha['titre']; ?></b></p>
<img src="<?php echo $donnees_cat['image']; ?>" width="100" class="img-droit" />
<p><?php echo $donnees_cat['description']; ?></p>


<p class="menu"><a href="index.php?page=textes&id=<?php echo $donnees_txt['id']; ?>">Lire la suite</a></p>
<?php
$req->closeCursor();
?>

